package com.guli.gulimall.gulimallproduct.service.impl;

import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;
import com.guli.gulimall.gulimallproduct.dao.SkuInfoDao;
import com.guli.gulimall.gulimallproduct.entity.SkuInfoEntity;
import com.guli.gulimall.gulimallproduct.service.SkuInfoService;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {
    @Autowired
    private  SkuInfoDao skuInfoDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuInfo(SkuInfoEntity skuInfoEntity) {
        this.baseMapper.insert(skuInfoEntity);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        QueryWrapper<SkuInfoEntity> wrapper = new QueryWrapper<>();
//        key:
//        catelogId: 0
//        brandId: 0
//        min: 0
//        max: 0

        String key = (String) params.get("key");
        if (null != key && !key.isEmpty() && !"0".equalsIgnoreCase(key)) {
            wrapper.and(w -> {
                w.eq("sku_id", key).or().like("sku_name", key);
            });
        }

        String brandId = (String) params.get("brandId");
        if (null != brandId && !brandId.isEmpty() && !"0".equalsIgnoreCase(brandId)) {
            wrapper.and(w -> {
                w.eq("brand_id", brandId);
            });
        }
        String catelogId = (String) params.get("catelogId");
        if (null != catelogId && !catelogId.isEmpty() && !"0".equalsIgnoreCase(catelogId)) {
            wrapper.and(w -> {
                w.eq("catalog_id", catelogId);
            });
        }
        String min = (String) params.get("min");
        if (null != min && !min.isEmpty()) {
            wrapper.and(w -> {
                w.ge("price", min);
            });
        }
        String max = (String) params.get("max");
        if (null != max && !max.isEmpty() && !"0".equalsIgnoreCase(max)) {
            wrapper.and(w -> {
                w.le("price", max);
            });
        }
        IPage<SkuInfoEntity> page = this.page(new Query<SkuInfoEntity>().getPage(params), wrapper);
        return new PageUtils(page);
    }

    @Override
    public List<SkuInfoEntity> getSkusBySpuId(Long spuId) {


        return  this.list( new QueryWrapper<SkuInfoEntity>().eq("spu_id",spuId));
    }

}